package com.yjck.fm.login.action;

import java.io.ByteArrayInputStream;
import java.io.InputStream;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.yjck.fm.login.bean.User;
import com.yjck.fm.login.service.LoginService;
import com.yjck.fm.util.MD5Utils;

@SuppressWarnings("serial")
public class LoginAction extends ActionSupport {
	private String email;
	private String passwd;
	private InputStream inputStream;
	private LoginService loginService;
	public void setLoginService(LoginService loginService) {
		this.loginService = loginService;
	}
	public InputStream getInputStream() {
	    return inputStream;
	}	
	public String getEmail() {
		return email;
	}


	public void setEmail(String email) {
			this.email = email;		
	}


	public String getPasswd() {
		return passwd;
	}


	public void setPasswd(String passwd) {
		this.passwd = passwd;
	}

	public String login() throws Exception {
		String password = MD5Utils.getMD5Str(passwd);
		if(loginService.validate(this.email,password)){
			User user = loginService.getUser(this.email);
			inputStream = new ByteArrayInputStream("success".getBytes("UTF-8"));
			ServletActionContext.getRequest().getSession().setAttribute("loginflag", "yjck67");
			ServletActionContext.getRequest().getSession().setAttribute("email", email);
			ServletActionContext.getRequest().getSession().setAttribute("id", user.getId());
			ServletActionContext.getRequest().getSession().setAttribute("name", user.getName());
			ServletActionContext.getRequest().getSession().setAttribute("sex", user.getSex());
		}else{
			inputStream = new ByteArrayInputStream("fail".getBytes("UTF-8"));
		}
		return "flag";
	}
}
